﻿using DeviceMonitor.Classes;
using DeviceMonitor.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace DeviceMonitor
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        public static Config config = null;
        private static object _lock = new object();
        public static bool stopCanbusThread = false;
        public static string checkCard = "0";

        public static List<CanBusNet> canBusNetList = null;

        public static List<DeviceGroupModel> deviceGroups = null;

        public static String globalDeviceGroupCode = "01";

        public static Window MainWindow = null;
        public static Config GetConfig()
        {
            if (config == null)
            {
                lock (_lock)
                {
                    if (config == null)
                    {
                        config = new Config();
                    }
                }
            }
            return config;
        }

        public static List<DeviceGroupModel> GetDeviceGroups()
        {
            if (deviceGroups == null)
            {
                lock (_lock)
                {
                    if (deviceGroups == null)
                    {
                        deviceGroups = new List<DeviceGroupModel>();
                    }
                }
            }
            return deviceGroups;
        }
        public static List<CanBusNet> GetCanBusNetList()
        {
            if (canBusNetList == null)
            {
                lock (_lock)
                {
                    if (canBusNetList == null)
                    {
                        canBusNetList = new List<CanBusNet>();
                    }
                }
            }
            return canBusNetList;
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            App.GetConfig().HttpServerIp = ConfigurationManager.AppSettings["HttpServerIp"];
            App.GetConfig().HttpServerPort = ConfigurationManager.AppSettings["HttpServerPort"];
            App.GetConfig().HttpCenterServerPort = ConfigurationManager.AppSettings["HttpCenterServerPort"];

            App.GetConfig().CheckCard = ConfigurationManager.AppSettings["CheckCard"];

            App.GetConfig().timerCheckSeconds = int.Parse(ConfigurationManager.AppSettings["timerCheckSeconds"]);
            App.GetConfig().timerUploadBillSeconds = int.Parse(ConfigurationManager.AppSettings["timerUploadBillSeconds"]);
            App.GetConfig().timerUploadAlermHours = int.Parse(ConfigurationManager.AppSettings["timerUploadAlermHours"]);
            
            App.GetConfig().uploadBill = ConfigurationManager.AppSettings["uploadBill"];
            App.GetConfig().uploadAlarm = ConfigurationManager.AppSettings["uploadAlarm"];

            App.GetConfig().SqliteDB = ConfigurationManager.AppSettings["SqliteDB"];

            App.GetConfig().sendMessage = ConfigurationManager.AppSettings["sendMessage"];

            App.GetConfig().messageShowSetting = ConfigurationManager.AppSettings["messageShowSetting"];
            App.GetConfig().messageDealSetting = ConfigurationManager.AppSettings["messageDealSetting"];

            App.GetConfig().DBServiceIp = ConfigurationManager.AppSettings["DBServiceIp"];
            App.GetConfig().DBServicePort = ConfigurationManager.AppSettings["DBServicePort"];

            App.GetConfig().webSocket = ConfigurationManager.AppSettings["webSocket"];
            App.GetConfig().UploadManually = ConfigurationManager.AppSettings["UploadManually"];
            
            int groupCount = int.Parse(ConfigurationManager.AppSettings["deviceGroups"]);
            
            for(int i=1; i<= groupCount; i++)
            {
                string code = i.ToString().PadLeft(2,'0');

                string groupName = ConfigurationManager.AppSettings["deviceGroup_" + code + "_Name"];
                string groupServerIp = ConfigurationManager.AppSettings["deviceGroup_" + code + "_Server_Ip"];
                string groupServerPort = ConfigurationManager.AppSettings["deviceGroup_" + code + "_Server_Port"];

                DeviceGroupModel groupModel = new DeviceGroupModel();
                groupModel.name = groupName;
                groupModel.serverIp = groupServerIp;
                groupModel.port = groupServerPort;
                groupModel.devices = new List<DeviceModel>();

                string deviceIds = ConfigurationManager.AppSettings["deviceGroup_" + code + "_Ids"];
                string[] idsArray = deviceIds.Split(',');

                Hashtable devicesTable = new Hashtable();
                int idCode = 0;
                foreach(string id in idsArray)
                {
                    DeviceModel device = new DeviceModel();
                    device.code = id;

                    groupModel.devices.Add(device);

                    devicesTable.Add(id, idCode);
                    idCode++;
                }
                groupModel.devicesTable = devicesTable;
                GetDeviceGroups().Add(groupModel);

                GetCanBusNetList().Add(new CanBusNet(groupModel.serverIp, int.Parse(groupModel.port), i-1));
            }
            
            string alermNameStrings = ConfigurationManager.AppSettings["alermName"];
            string[] alermNameArray = alermNameStrings.Split(',');

            App.GetConfig().alermNameMap = new Hashtable();
            int idx = 0;
            foreach(string alermName in alermNameArray)
            {
                App.GetConfig().alermNameMap.Add(idx.ToString(), alermName);
                idx++;
            }
        }
    }
}
